package com.situ.water_BE.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.util.addBe_History;
import com.situ.water_BE.mapper.BeFormulaMapper;
import com.situ.water_BE.mapper.BeOrderuserMapper;
import com.situ.water_BE.pojo.BeFormula;
import com.situ.water_BE.pojo.BeOrder;
import com.situ.water_BE.pojo.BeOrderuser;
import com.situ.water_BE.service.BeFormulaService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author Angel
 * @since 2020-12-01
 */
@Service
public class BeFormulaServiceImpl extends ServiceImpl<BeFormulaMapper, BeFormula> implements BeFormulaService {
    @Autowired
    private BeFormulaMapper dao;
    @Autowired
    private BeOrderuserMapper beOrderuserDao;

    @Override
    public int addBeFormula(List<BeOrderuser> list, BeOrder beOrder) {
        List<BeOrderuser> orderNo = beOrderuserDao.selectList(new QueryWrapper<BeOrderuser>().eq(true
                , "orderNo", beOrder.getOrderNo()));
        for (BeOrderuser l : list) {
            beOrderuserDao.update(l, new UpdateWrapper<BeOrderuser>().eq(true,
                    "orderNo", beOrder.getOrderNo()));
        }
        for (int i = 0; i < list.size(); i++) {
            List<BeFormula> beFormulas = dao.selectList(new QueryWrapper<BeFormula>().
                    eq("OrderUserID", orderNo.get(i).getId()));
            System.out.println(beFormulas.size());
            if(beFormulas!=null && beFormulas.size()>0){
                dao.delete(new QueryWrapper<BeFormula>().eq("OrderUserID",
                        orderNo.get(i).getId()));
            }
            String split = list.get(i).getFormula();
            String replace = split.replace("[", "");//.replace("]", "");
            String[] split1 = replace.split("]");
            for (int j = 0; j < split1.length; j++) {
                System.out.println(split1[j]);
                if (split1[j].contains("%")) {
                    dao.insert(addBe_History.addBeFormula(orderNo.get(i).getId(),
                            split1[j].split(":")[0], split1[j].split(":")[1],
                            1, Integer.parseInt(split1[j].split(":")[1].substring(0, split1[j].split(":")[1].length() - 1))));
                    System.out.println("1");
                } else if (split1[j].contains("*")) {
                    dao.insert(addBe_History.addBeFormula(orderNo.get(i).getId(),
                            split1[j].split(":")[0], split1[j].split(":")[1],
                            2, -1));
                    System.out.println("2");
                } else {
                    dao.insert(addBe_History.addBeFormula(orderNo.get(i).getId(),
                            split1[j].split(":")[0], split1[j].split(":")[1],
                            0, Integer.parseInt(split1[j].split(":")[1])));
                    System.out.println("0");
                }
            }
        }
        return 1;
    }
}
